package com.iot08.api.interceptor;

import com.alibaba.fastjson.JSON;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @version 1.0
 * @Description:
 * @time: 2021/1/27 18:47
 * @author: Rock
 */
@Slf4j
public class BaseInterceptor extends HandlerInterceptorAdapter {

    /**
     * 设置统一返回格式
     * @param response
     * @param data
     * @time: 2021/1/27 18:36
     * @author: Rock
     * @version 1.0
     */
    protected void returnJson(HttpServletResponse response, ResponseMap data) {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(JSON.toJSON(data));
            writer.flush();
        } catch (IOException e) {
            log.error("returnJson is IOException.");
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }




}
